package com.qing.design.patterns.strategy.example;

import com.qing.design.patterns.strategy.example.order.Order;
import com.qing.design.patterns.strategy.example.strategies.PayByCreditCard;
import com.qing.design.patterns.strategy.example.strategies.PayByPayPal;
import com.qing.design.patterns.strategy.example.strategies.PayStrategy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class Demo {
    //价格产品
	private static Map<Integer,Integer> priceOnProducts=new HashMap<>();

	private  static BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));

	private static Order order=new Order();
	private static PayStrategy payStrategy;

	static {
		priceOnProducts.put(1,1100);
		priceOnProducts.put(2,2200);
		priceOnProducts.put(3,3300);
		priceOnProducts.put(4,4400);
	}

	public static void main(String[] args) throws IOException {
		while (!order.isClosed()) {
			int cost;

			String continueChoice;
			do{
				System.out.println("请选择一个产品：+\n" +
						"“1-主板”+\n" +
						"“2-CPU”+\n" +
						"“3-硬盘”+\n" +
						"“4-内存”");
				int choice=Integer.parseInt(reader.readLine());
				cost=priceOnProducts.get(choice);
				System.out.println("Count:");
				int count=Integer.parseInt(reader.readLine());

				order.setTotalCost(cost*count);
				System.out.println("是否要继续选择产品？是/否：");
				continueChoice= reader.readLine();
			}while (continueChoice.equalsIgnoreCase("y"));

			if(payStrategy==null){
				System.out.println("请选择支付方式\n");
				System.out.println("1.PayPly 2.银行卡");
				String payMethod= reader.readLine();

				//客户根据用户的输入创建不同的策略，
				//应用程序配置等。
				if(payMethod.equalsIgnoreCase("1")){
					payStrategy=new PayByPayPal();
				}else{
					payStrategy=new PayByCreditCard();
				}
			}
//			订单对象将收集付款数据委托给策略对象，
			//因为只有策略知道他们需要什么数据来处理
			//付款。
			order.processOrder(payStrategy);

			System.out.println("支付"+order.getTotalCost()+"还是继续购物？p/c");
			if(reader.readLine().equalsIgnoreCase("p")){
				//最后，策略处理付款。
				if(payStrategy.pay(order.getTotalCost())){
					System.out.println("支付成功！");
				}else{
					System.out.println("支付失败！");
				}
				order.setClosed();
			}
		}



	}
}
